import os, sys
base_dir=os.path.abspath(os.path.dirname(__file__));
rootPath=os.path.split(base_dir);
sys.path.append(base_dir);

from symbol.base_symbol import *
'''
	collections是Python内建的一个集合模块，提供了许多有用的集合类和方法。
	print(dir(collections))
'''
from collections import OrderedDict

# 作用域符号表

class ScopedSymbolTable(object):

	def __init__(self, scope_name, scope_level):
		self._symbols = OrderedDict()
		self.scope_name = scope_name
		self.scope_level = scope_level
		self._init_builtins()

	def _init_builtins(self):
		self.insert(BuiltinTypeSymbol('INTEGER'))
		self.insert(BuiltinTypeSymbol('REAL'))

	def __str__(self):
		h1 = 'SCOPE (作用域符号表)'
		lines = ['\n', h1, '=' * len(h1)]
		for header_name, header_value in (
			('Scope name', self.scope_name),
			('Scope level', self.scope_level),
		):
			lines.append('%-15s: %s' % (header_name, header_value))
		h2 = 'Scope (作用域符号表) contents'
		lines.extend([h2, '-' * len(h2)])
		lines.extend(
			('%7s: %r' % (key, value))
			for key, value in self._symbols.items()
		)
		lines.append('\n')
		s = '\n'.join(lines)
		return s

	__repr__ = __str__

	# 添加到作用域
	def insert(self, symbol):
		print('添加到作用域: %s' % symbol.name)
		self._symbols[symbol.name] = symbol
	# 查找作用域
	def lookup(self, name):
		print('查找作用域: %s' % name)
		symbol = self._symbols.get(name)
		# 'symbol' is either an instance of the Symbol class or None
		return symbol